﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.Ctrls;
using CommunitySystem.Views;
using System.Xml.Linq;

namespace CommunitySystem.PageView.AdvancedCustomerQuery
{
    public partial class SpecifyQueryResult : AsyncCtrl, IReturnListenerc
    {
        private CustomerQuery.CustomerQueryClient _client = null;
        private CustomerizeControl _parent = null;
        private CommunitySystem.CustomerQuery.Query_CustomerQuery _item = null;
        private string _ids= null;
        public SpecifyQueryResult(CommunitySystem.CustomerQuery.Query_CustomerQuery item, string ids, CustomerizeControl parent)
        {
            InitializeComponent();
            _client = new CustomerQuery.CustomerQueryClient();
            _parent = parent;
            _item = item;
            _ids = ids;
            _client.GetRecordsCompleted -= new EventHandler<CustomerQuery.GetRecordsCompletedEventArgs>(client_GetRecordsCompleted);
            _client.GetRecordsCompleted += new EventHandler<CustomerQuery.GetRecordsCompletedEventArgs>(client_GetRecordsCompleted);
            _client.QueryDataCompleted += new EventHandler<CustomerQuery.QueryDataCompletedEventArgs>(_client_QueryDataCompleted);
            _client.GetRecordsAsync(item.QueryType);
        }

        void _client_QueryDataCompleted(object sender, CustomerQuery.QueryDataCompletedEventArgs e)
        {
            this.LayoutRoot.Children.Clear();
            _parent.xBusy.IsBusy = false;
            _parent._docx = XDocument.Parse(e.Result);
            _parent.DrawGrid(this.LayoutRoot, _parent._docx);
        }

        void client_GetRecordsCompleted(object sender, CustomerQuery.GetRecordsCompletedEventArgs e)
        {
            cbtype.ItemsSource = null;
            cbtype.ItemsSource = e.Result;
            cbtype.SelectedIndex = 0;
        }

        public void BeginReturn()
        {
            return;
        }

        private void cbtype_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cbtype.SelectedValue != null && e.RemovedItems != null  && e.RemovedItems.Count > 0)
            {
                int Year = int.Parse(cbtype.SelectedValue.ToString());
                _parent.xBusy.IsBusy = true;
                _client.QueryDataAsync(_item.ID, _ids, Year);
            }
        }
    }
}
